package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.mapper.MarketCartMapper;
import com.cskaoyan.market.service.WxCouponService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.WxCouponVo;
import com.cskaoyan.market.vo.WxMyCouponVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;
    @Autowired
    SecurityManager securityManager;

    @Autowired
    MarketCartMapper marketCartMapper;


    @GetMapping("list")
    public Object list(Integer page,Integer limit){
        List<WxCouponVo> list=wxCouponService.list(page,limit);
        Object o = ResponseUtil.okList(list);
        return o;
    }

    @GetMapping("mylist")
    public Object myList(Short status,Integer page,Integer limit){
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");

        List<WxMyCouponVo> list=wxCouponService.myList(user,status,page,limit);
        Object o = ResponseUtil.okList(list);
        return o;
    }

    @GetMapping("selectlist")
    public Object selectList(Integer cartId,Integer grouponRulesId){
        Integer userId=0;
        if (cartId==0){
            SecurityUtils.setSecurityManager(securityManager);
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            MarketUser user = (MarketUser) session.getAttribute("user");
            userId=user.getId();
        }
        List<WxMyCouponVo> list=wxCouponService.selectList(userId,cartId,grouponRulesId);
        Object o = ResponseUtil.okList(list);
        return o;
    }

    @PostMapping("receive")
    public Object receive(@RequestBody Map<String,Integer> data){
        Integer couponId = data.get("couponId");
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");

        Integer code=wxCouponService.receive(couponId,user);
        String message="";
        if (code==740){
            message="优惠卷已经领取过";
        }
        if (code==744){
            message="优惠卷不可用";
        }
        if (code==0){
            message="成功";
        }
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("errno", code);
        obj.put("errmsg", message);
        return obj;
    }
    @PostMapping("exchange")
    public Object exchange(@RequestBody Map<String ,String> data){
        String i = data.get("code");
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        Integer message=wxCouponService.exchange(i,user);
        String msg="";
        if (message==742){
            msg="优惠卷不正确";
        }
        if (message==740){
            msg="优惠卷已兑换";
        }
        if (message==744){
            msg="优惠卷已过期";
        }
        if (message==0){
            msg="优惠卷兑换成功";
        }
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("errno", message);
        obj.put("errmsg", msg);
        return obj;
    }
}
